package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

@Component
public class UserController{
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        Scanner scan=new Scanner(System.in);
        System.out.println("1:注册,2:登录,3:修改用户信息,4用户列表,5:删除用户");
        int num=Integer.parseInt(scan.nextLine());
        switch (num){
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updataUser();
                break;
            case 4:
                listUser();
                break;
            case 5:
                deleteUser();
                break;
            default:
                System.out.println("其他功能开发中");

        }

    }
    public void deleteUser(){
        System.out.println("请输入要删除的用户名");
        Scanner scan=new Scanner(System.in);
        String userName=scan.nextLine();
        if (userMapper.selectByUserName(userName)!=null){
            int num=userMapper.deleteByUsername(userName);
            System.out.println(num>0?"删除成功":"删除失败");
        }else {
            System.out.println("用户名不存在");
        }
    }
    public void listUser(){
        System.out.println("用户列表");
        List<User> userList=userMapper.selectAll();
        for (User user:userList){
            System.out.println(user);
        }
    }
    public void updataUser(){
        System.out.println("请输入您要修改的用户的用户名");
        Scanner scan=new Scanner(System.in);
        String userName=scan.nextLine();
        System.out.println("请输入新密码");
        String password=scan.nextLine();
        System.out.println("请输入新昵称");
        String nickname=scan.nextLine();
        System.out.println("请输入新年龄");
        int age=scan.nextInt();
        if (userMapper.selectByUserName(userName)!=null) {
            User user = new User(null, userName, password, nickname, age);
            int num = userMapper.updata(user);
            System.out.println(num > 0 ? "修改成功" : "修改失败");
        }else {
            System.out.println("用户名不存在");
        }
    }
    public void regUser(){
        System.out.println("开始处理注册");
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入用户名");
        String userName=scan.nextLine();
        System.out.println("请输入密码");
        String password=scan.nextLine();
        System.out.println("请输入昵称");
        String nickname=scan.nextLine();
        System.out.println("请输入年龄");
        int  age=scan.nextInt();
        if (userMapper.selectByUserName(userName)!=null){
            System.out.println("用户名已存在");
        }else {
            User user =new User(null,userName,password,nickname,age);
            int num=userMapper.insert(user);
            System.out.println(num>0?"注册成功":"注册失败");
        }

    }
    public void loginUser(){
        System.out.println("开始处理登录");
        Scanner scan=new Scanner(System.in);
        System.out.println("请输入用户名");
        String userName=scan.nextLine();
        System.out.println("请输入密码");
        String passWord=scan.nextLine();
        if (userMapper.selectByUserNameAndPassword(userName,passWord)!=null){
            System.out.println("登录成功");
            System.out.println(userMapper.selectByUserNameAndPassword(userName,passWord));
        }else {
            System.out.println("用户名或密码错误");
        }
    }
}
